<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<link href="../style/css/manual-zip.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-zip-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
<title>URL重写简介 － Apache 2.2 中文手册 [金步国]</title>
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d286c55b63a3c54a1e43d10d4c203e75"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
</head>
<body id="manual-page"><div id="page-header">
<p class="menu"><a href="../mod/index.html">模块索引</a> | <a href="../mod/directives.html">指令索引</a> | <a href="../faq/index.html">常见问题</a> | <a href="../glossary.html">词汇表</a> | <a href="../sitemap.html">站点导航</a></p><p class="apache">Apache HTTP Server 版本2.2</p><img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="index.html"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
<div id="path"><a href="https://www.apache.org/">Apache</a> &gt; <a href="https://httpd.apache.org/">HTTP Server</a> &gt; <a href="https://httpd.apache.org/docs/">文档</a> &gt; <a href="../index.html">版本2.2</a> &gt; <a href="index.html">URL重写</a></div>

<div id="translation-info">　　 <a href="../translator_announcement.html#thanks">致谢</a> | 本篇译者：<a href="../../../index.html">金步国</a>(<a href="../../../index.html">作品集</a>) | 本页最后更新：2006年10月20日</div>
<div id="page-content"><div id="preamble"><h1>URL重写简介</h1>


<p>本篇文档是<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code><a href="../mod/mod_rewrite.html">参考文档</a>的补充，阐述了使用<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>之前必须了解的基本概念。其它文档则作了更加深入的探讨。不过这篇文档对于初学者是一篇很好的入门读物。</p>
</div>
	<div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="introduction" id="introduction">简介</a></h2>
<p>Apache的<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>是提供了强大URL操作的杀手级模块，可以实现几乎所有你梦想的URL操作，其代价是你必须接受其复杂性，因为<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>对于初学者的主要障碍就是不容易理解和运用，即使是Apache专家有时也会发掘出<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>的新用途。换句话说：你或者是打退堂鼓永不再用，或者是喜欢它并一生受用。目前存在这样一种倾向：许多初学者只是把URL重写规则当着是会变戏法的魔咒，而并未在使用中真正理解这些规则的含义。</p>

<p>本篇文档试图给出充分的背景知识，以便于初学者随后的理解，而不是盲目的复制和粘贴。</p>
</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="regex" id="regex">正则表达式</a></h2>

<p><code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>使用的是<a href="http://pcre.org/">Perl兼容的正则表达式</a>语法。本文不打算详细讲解正则表达式语法，你可以到<a href="http://pcre.org/pcre.txt">PCRE man page</a>, <a href="http://www.perldoc.com/perl5.8.0/pod/perlre.html">Perl regular expression man page</a>, <a href="http://www.oreilly.com/catalog/regex2/index.html">Mastering Regular Expressions, by Jeffrey Friedl</a>获得这些内容。</p>

<p><code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>指令的说明部分有一个简单的正则表达式语法简介，可以去参考一下。<br>另外需要说明的是可以在表达式的最前面加上一个感叹号('!')表示<strong>不匹配</strong>，不过这种用法并不符合正则表达式语法。</p>

<h3><a name="InternalBackRefs" id="InternalBackRefs">正则表达式的反向引用能力</a></h3>

      <p>这是很重要的一点：一旦在<em>Pattern</em>或者<em>CondPattern</em>中使用了圆括号，就会建立内部的反向引用，可以使用<code>$N</code>和<code>%N</code>来调用(见下述)，并且在<em>Substitution</em>和<em>TestString</em>中都有效。图-2说明了反向引用被转换和展开的位置。</p>

<p class="figure">
      <img src="../images/mod_rewrite_fig2.gif" width="381" height="179" alt="[Needs graphics capability to display]" /><br />
      <dfn>图-2:</dfn> The back-reference flow through a rule.
</p>


</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="rewriterule" id="rewriterule">RewriteRule basics</a></h2>
<p>(此部分文档尚未完成)
Basic anatomy of a RewriteRule, with exhaustively annotated simple
examples.
</p>
</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="flags" id="flags">Rewrite Flags</a></h2>
<p>(此部分文档尚未完成)
Discussion of the flags to RewriteRule, and when and why one might
use them.</p>
</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="rewritecond" id="rewritecond">Rewrite conditions</a></h2>
<p>(此部分文档尚未完成)
Discussion of RewriteCond, looping, and other related concepts.
</p>
</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="rewritemap" id="rewritemap">Rewrite maps</a></h2>
<p>(此部分文档尚未完成)
Discussion of RewriteMap, including simple, but heavily annotated,
examples.</p>
</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="htaccess" id="htaccess">.htaccess files</a></h2>
<p>(此部分文档尚未完成)
Discussion of the differences between rewrite rules in httpd.conf and
in .htaccess files.</p>
</div><div class="top"><a href="rewrite_intro.html#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="EnvVar" id="EnvVar">环境变量</a></h2>

<p>此模块会跟踪两个额外的(非标准)CGI/SSI环境变量：<code>SCRIPT_URL</code>和<code>SCRIPT_URI</code>。他们包含了当前资源的<em>逻辑</em>网络视图，而标准CGI/SSI变量<code>SCRIPT_NAME</code>和<code>SCRIPT_FILENAME</code>包含的是<em>物理</em>系统视图。</p>

<p>注意：这些变量保持的是<em>其最初被请求时的</em>URI/URL，即在任何重写操作<em>之前</em>的URI/URL。其重要性在于他们是重写操作重写URL到物理路径名的原始依据。</p>

<div class="example"><h3>示例</h3><pre>
SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.html
SCRIPT_FILENAME=/u/rse/.www/index.html
SCRIPT_URL=/u/rse/
SCRIPT_URI=http://en1.engelschall.com/u/rse/
</pre></div>

</div></div>
<div id="footer">
<p class="apache">本文允许自由的转载、引用、再分发，但必须保留译者署名并注明出处；详见：<a href="../translator_announcement.html#announcement">版权声明</a>。</p>
<p class="menu"><a href="../mod/index.html">模块索引</a> | <a href="../mod/directives.html">指令索引</a> | <a href="../faq/index.html">常见问题</a> | <a href="../glossary.html">词汇表</a> | <a href="../sitemap.html">站点导航</a></p></div>
</body></html>
